/**
 * 
 */
package com.whale;

import java.util.ArrayList;
import java.util.concurrent.Callable;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.widget.ListView;
import com.whale.activity.WhaleBaseActivity;
import com.whale.adapter.TrendListAdapter;
import com.whale.constants.WhaleConstants;
import com.whale.entity.Stream;
import com.whale.util.async.Callback;
import com.whale.widget.RefreshView;
import com.whale.widget.SeeMoreView;

/**
 * @author lzk
 */

public class TrendsActivity extends WhaleBaseActivity {
	private TrendListAdapter ada_mainStream;//数据适配器
	private ListView lv_stream;
	private ArrayList<Stream> mainStreamList;//主墙数据流

	private RefreshView refreshView;//刷新的视图
	private SeeMoreView seeMoreView;//查看更多的视图

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.trends);
		LayoutInflater inflater = LayoutInflater.from(this);
		View view = (ViewGroup) inflater.inflate(R.layout.stream, null, false);
		lv_stream = (ListView) view.findViewById(R.id.lv_stream);
		// MainStream 刷新
		refreshView = new RefreshView(this);
		refreshView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				lv_stream.removeFooterView(seeMoreView);
				refreshView.setProgressBarActive(true);
				if (mainStreamList != null) {
					mainStreamList.clear();
				}
				asyncAddMainStream(true);
			}
		});
		lv_stream.addHeaderView(refreshView);
		lv_stream.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN
						|| keyCode == KeyEvent.KEYCODE_DPAD_UP) {
					return true;
				} else {
					return false;
				}
			}
		});
		// MainStream 更多
		seeMoreView = new SeeMoreView(this);
		seeMoreView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				seeMoreView.setProgressBarActive(true);
				asyncAddMainStream(false);
			}
		});
		lv_stream.addFooterView(seeMoreView);

		// 数据适配器
		ada_mainStream = new TrendListAdapter(this);
		lv_stream.setAdapter(ada_mainStream);
		WhaleApplication.bNeedRefreshHome = true;
	}

	@Override
	protected void onStart() {
		super.onStart();
		if (WhaleApplication.bNeedRefreshHome) {
			refreshView.performClick();
			WhaleApplication.bNeedRefreshHome = false;
		}
	}

	/**
	 * 增量增加MainStream评论
	 * 
	 * @param isRefresh
	 */
	private void asyncAddMainStream(final boolean isRefresh) {
		this.doAsync(false, new Callable<Integer>() {
			@Override
			public Integer call() {
				Integer retSize = 0;
				try {
					if (mainStreamList == null) { // first time load
						mainStreamList = (ArrayList<Stream>) getApiService()
								.viewSelfWall("", WhaleConstants.FEEDS_NUM_ONCE).data;
						retSize = mainStreamList.size();
					} else if (mainStreamList.size() == 0) {
						ArrayList<Stream> addStreamList = (ArrayList<Stream>) getApiService()
								.viewSelfWall("", WhaleConstants.FEEDS_NUM_ONCE).data;
						retSize = addStreamList.size();
						mainStreamList.addAll(addStreamList);
					} else {
						ArrayList<Stream> addStreamList = (ArrayList<Stream>) getApiService()
								.viewSelfWall("", WhaleConstants.FEEDS_NUM_ONCE).data;
						retSize = addStreamList.size();
						mainStreamList.addAll(addStreamList);
					}
				} catch (Exception npe) {
					npe.printStackTrace();
					return -1;
				}
				return retSize;
			}
		}, new Callback<Integer>() {
			@Override
			public void onCallback(Integer retSize) {

				ada_mainStream.setCopyList(mainStreamList);

				if (isRefresh) {
					refreshView.setProgressBarActive(false);

					if (retSize > 0)
						lv_stream.addFooterView(seeMoreView);
					else if (retSize == 0) {
						refreshView.getTextView().setText(R.string.nodata);
					}

					lv_stream.postDelayed(new Runnable() {
						@Override
						public void run() {
							lv_stream.setSelection(1);
						}
					}, WhaleConstants.DELAY_MIL);

				} else {
					seeMoreView.setProgressBarActive(false);
				}
				if (retSize < 0) {
					refreshView.getTextView().setText(R.string.neterror);
				} else if (retSize < WhaleConstants.FEEDS_NUM_ONCE) {
					seeMoreView.getTextView().setText(R.string.nomore);
				}
			}
		});
	}

}
